package com.lb.pdm.controller;

import com.lb.pdm.common.result.GraceJSONResult;
import com.lb.pdm.entity.HouseholdMember;
import com.lb.pdm.entity.HouseholdTransfer;
import com.lb.pdm.entity.bo.*;
import com.lb.pdm.entity.vo.HouseholdInfoVO;
import com.lb.pdm.entity.vo.HouseholdInfoVO2;
import com.lb.pdm.entity.vo.HouseholdMemberVO;
import com.lb.pdm.entity.vo.HouseholdTransferVO;
import com.lb.pdm.service.BirthRegistrationService;
import com.lb.pdm.service.HouseholdInfoService;
import com.lb.pdm.service.HouseholdMemberService;
import com.lb.pdm.service.HouseholdTransferService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: HouseholdController
 * @Author Wood
 * @Package com.lb.pdm.controller
 * @Date 2025/10/29 19:13
 * @description:
 */
@RestController
@RequestMapping("/household")
@Tag(name = "户籍管理", description = "户籍相关业务接口")
public class HouseholdController {
    @Autowired
    private HouseholdInfoService householdInfoService;
    @Autowired
    private HouseholdMemberService householdMemberService;
    @Autowired
    private BirthRegistrationService birthRegistrationService;
    @Autowired
    private HouseholdTransferService householdTransferService;

    @PostMapping("/register")
    @Operation(summary = "家庭户口立户", description = "创建新的家庭户口")
    public GraceJSONResult registerHousehold(@RequestBody HouseholdInfoBO householdInfoBO, @RequestHeader("token") String token) {
        return householdInfoService.registerHousehold(householdInfoBO, token);
    }

    @GetMapping("/list")
    @Operation(summary = "户口综合查询", description = "查询户口信息列表")
    public GraceJSONResult listHousehold(@RequestParam(required = false) String name,
                                         @RequestParam(required = false) String idCard,
                                         @RequestParam(required = false) String address,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        // 构建查询条件对象
        HouseholdQueryBO queryBO = new HouseholdQueryBO();
        queryBO.setName(name);
        queryBO.setIdCard(idCard);
        queryBO.setAddress(address);
        queryBO.setPageNum(pageNum);
        queryBO.setPageSize(pageSize);
        
        List<HouseholdMemberVO> householdMemberVOS = householdMemberService.listHouseholds(queryBO);
        return GraceJSONResult.ok(householdMemberVOS);
    }

    @GetMapping("/queryHouseholder")
    @Operation(summary = "查询户主信息", description = "查询户主信息")
    public GraceJSONResult queryHouseholder(@RequestParam(required = false) String name,
                                            @RequestParam(required = false) String idCard,
                                            @RequestParam(required = false) String address,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "10") Integer pageSize) {
        HouseholderBO householderBO = new HouseholderBO();
        householderBO.setName(name);
        householderBO.setIdCard(idCard);
        householderBO.setAddress(address);
        householderBO.setPageNum(pageNum);
        householderBO.setPageSize(pageSize);
        List<HouseholdInfoVO> householdInfoVOS = householdInfoService.queryHouseholder(householderBO);
        return GraceJSONResult.ok(householdInfoVOS);
    }

    @GetMapping("/getPersonHousehold/{personId}")
    @Operation(summary = "查询个人所属的户口信息", description = "查询个人所属的户口信息")
    public GraceJSONResult getPersonHousehold(@PathVariable Long personId) {
        System.out.println("DEBUG: 控制器接收到查询个人户籍请求, personId = " + personId);
        HouseholdInfoVO2 householdInfoVO = householdInfoService.getPersonHousehold(personId);
        System.out.println("DEBUG: 控制器返回结果 = " + householdInfoVO);
        return GraceJSONResult.ok(householdInfoVO);
    }

    @PostMapping("/birthRegister")
    @Operation(summary = "出生申报", description = "出生申报")
    public GraceJSONResult birthRegister(@RequestBody BirthRegistrationBO birthRegistrationBO, @RequestHeader("token") String token) {
        return birthRegistrationService.birthRegister(birthRegistrationBO,  token);
    }

    @GetMapping("/getHouseholdTransfer")
    @Operation(summary = "查询户口迁移信息", description = "查询户口迁移信息")
    public GraceJSONResult getHouseholdTransfer(@RequestParam(required = false) String name,
                                                @RequestParam(required = false) String idCard,
                                                @RequestParam(required = false) Byte status,
                                                @RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "10") Integer pageSize) {
        HouseholdTransferBO householdTransferBO = new HouseholdTransferBO(name, idCard, status, pageNum, pageSize);
        List<HouseholdTransferVO> householdTransferVOS = householdTransferService.getHouseholdTransfer(householdTransferBO);
        return GraceJSONResult.ok(householdTransferVOS);
    }

    @GetMapping("/getHouseholdTransferDetail/{transferId}")
    @Operation(summary = "查询户口迁移详情", description = "查询户口迁移详情")
    public GraceJSONResult getHouseholdTransferDetail(@PathVariable Long transferId) {
        HouseholdTransferVO householdTransferVO = householdTransferService.getHouseholdTransferDetail(transferId);
        return GraceJSONResult.ok(householdTransferVO);
    }

    @PutMapping("/updateHouseholdTransfer")
    @Operation(summary = "审批户口迁移信息", description = "审批户口迁移信息")
    public GraceJSONResult updateHouseholdTransfer(@RequestBody ApprovalBO approvalBO, @RequestHeader("token") String token) {
        int i = householdTransferService.updateHouseholdTransfer(approvalBO, token);
        if (i <= 0) {
            return GraceJSONResult.errorMsg("审批失败");
        }
        return GraceJSONResult.ok();
    }

    @PostMapping("/applyTransfer")
    @Operation(summary = "申请户口迁移", description = "申请户口迁移")
    public GraceJSONResult applyTransfer(@RequestBody HouseholdTransferApplicationBO householdTransferApplicationBO, @RequestHeader("token") String token) {
        return householdTransferService.applyTransfer(householdTransferApplicationBO, token);
    }

    @PutMapping("/deathCancellation")
    @Operation(summary = "注销死亡人信息", description = "注销死亡人信息")
    public GraceJSONResult deathCancellation(@RequestParam("idCard") String idCard, @RequestHeader("token") String  token) {
        return householdMemberService.deathCancellation(idCard,  token);
    }
}
